<?php

namespace addons\rexam\controller;

use think\Session;

class User extends Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\rexam\User;
    }

    public function group(){
        if ($this->request->isAjax()) {
            $param = input();
            $data = array();
            $data['name'] = $param['user_name'];
            $data['rexam_group_id'] = $param['group_id'];
            $res = $this->model->save($data,['id'=> $this->rexam_user['id']]);
            return $this->ajax_success('更新成功!',array('res'=>$res));
        }
    }

    public function login(){
        if($this->auth->isLogin()){
            $where = array();
            $where['user_id']=$this->auth->id;
            $this->rexam_user = $this->model
                ->with(['sysuser'])
                ->where($where)
                ->find();
            if($this->rexam_user){
                $url = addon_url('rexam/index/index');
                $this->redirect($url);
            }else{
                $this->model->create(['user_id'=>$this->auth->id]);
                $url = addon_url('rexam/index/index');
                $this->redirect($url);
            }
        }
        $third = get_addon_config('third');
        if($this->config['third']  && !empty($third)){
            $this->assign('platform', $third['status']);
            $url = addon_url('third/index/connect',[':platform'=>'wechat']);
            $this->assign('url', $url);
        }else{
            $url = url('/index/user/login');
            $this->redirect($url);
        }
        return $this->view->fetch();
    }

    public function logout(){
        Session::set("rexam_user", null);
        $this->auth->logout();
        $url = addon_url('rexam/user/login');
        $this->redirect($url);
    }
}